﻿String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}


// example of using trim, ltrim, and rtrimvar myString = " hello my name is ";
//alert("*"+myString.trim()+"*");
//alert("*"+myString.ltrim()+"*");
//alert("*"+myString.rtrim()+"*");


//
//	用于数据校验
//
// 在onfocusout中验证网页数据录入的合法性（是否允许为空(为空不在这里检验),数字,日期,百分比）
// <script src="../../../../js/Common.js" type="text/javascript"></script>
// <asp:textbox id="txtRoomSize" runat="server" Width="80px" 
// onfocusout="return validateElement(this)" validateCondition="title=标题;isNecessary=1;isNumber=1;isDate=0;isRate=0"></asp:textbox>
function validateElement(element) {
    return true;
}

function validateElement2(element) {
    var objCondition = new validateConditions();
    var validates = element.getAttribute("validateCondition").split(";");
    for (i = 0; i < validates.length; i++) {
        var name = validates[i].split("=")[0];
        var value = validates[i].split("=")[1];
        switch (name) {
            case "title":
                objCondition.title = value;
                break;
            case "isNecessary":
                objCondition.isNecessary = value;
                break;
            case "isNumber":
                objCondition.isNumber = value;
                break;
            case "isDate":
                objCondition.isDate = value;
                break;
            case "isRate":
                objCondition.isRate = value;
                break;
            case "isInt":
                objCondition.isInt = value;
                break;
            default:
                break;
        }
    }

    if (element.disabled == false) {
        if (objCondition.isNecessary == "1") {
            if (element.value.trim() == "") {
                alert("请输入［" + objCondition.title + "］！");
                handleFocus(element);
                return false;
            }
        }
        if (objCondition.isNumber == "1") {
            if (element.value != "") {
                if (isNaN(element.value)) {
                    alert("［" + objCondition.title + "］必须是数字！");
                    handleFocus(element);
                    return false;
                }
            }
        }

        if (objCondition.isDate == "1") {
            if (element.value != "") {
                var strDate = element.value;
                if (!isDay(strDate)) {
                    alert("［" + objCondition.title + "］必须是有效的日期！");
                    handleFocus(element);
                    return false;
                }
            }
        }

        if (objCondition.isRate == "1") {
            if (element.value != "") {
                if (!isRate(element.value)) {
                    alert("［" + objCondition.title + "］必须是介于0到100的数字！");
                    handleFocus(element);
                    return false;
                }
            }
        }

        if (objCondition.isInt == "1") {
            if (element.value != "") {
                if (!isInt(element.value)) {
                    alert("［" + objCondition.title + "］必须是整数！");
                    handleFocus(element);
                    return false;
                }
            }
        }
    }
    return true;
}

function handleFocus(element) {
    try {
        element.focus();
    }
    catch (e) {
    }
}

function killError() {
    return true;
}

// karon 网页数据录入合法性条件
function validateConditions() {
    this.title = "";
    this.isNecessary = "";
    this.isNumber = "";
    this.isDate = "";
    this.isInt = "";
}

//验证是否日期(中原格式20010101)
function isDay(dateStr) {
    //alert (dateStr);
    //要先将八位数yyyymmdd形式转为yyyy-mm-dd形式再进行比较
    if (dateStr.length != 8) {
        return false;
    }
    dateStr = dateStr.substring(0, 4) + "-" + dateStr.substring(4, 6) + "-" + dateStr.substring(6, 8);
    //alert(dateStr);
    //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    //month = matchArray[1]; // p@rse date into variables
    //day = matchArray[3];
    //year = matchArray[5];
    year = matchArray[1];
    month = matchArray[3]; // p@rse date into variables
    day = matchArray[5];

    if (year < 1900) {
        return false;
    }
    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        //alert("Month "+month+" doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            //alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }

    return true; // date is valid
}

//验证是否日期(格式正常2001-01-01)
function isDate(dateStr) {
    //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    //month = matchArray[1]; // p@rse date into variables
    //day = matchArray[3];
    //year = matchArray[5];
    year = matchArray[1];
    month = matchArray[3]; // p@rse date into variables
    day = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        //alert("Month "+month+" doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            //alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

function isRate(rateStr) {
    if (rateStr < 0 || rateStr > 100) {
        return false;
    }
    return true;
}

function isInt(str) {
    if (str == "") {
        return true;
    }
    else if (isNaN(str)) {
        return false;
    }
    else if (/^(\-?)(\d+)$/.test(str)) {
        return true;
    }
    else {
        return false;
    }
}

function OnFormSubmit() {
    var form0 = document.getElementById("Form1") == null ? document.getElementById("form1") : document.getElementById("Form1");
    if (form0 == null) {
        form0 = document.getElementById("aspnetForm");
    }
    var element;
    if (form0 == null) { return false };
    for (j = 0; j < form0.length; j++) {
        element = form0.item(j);
        if (element.getAttribute("validateCondition") != null) {
            if (!validateElement2(element)) {
                return false;
            }
        }
    }
    return true;
}

function BeforeSave() {
    if (verifyMustField(document.frmEdit.elements) == false) {
        return false;
    }
    if (verifyIsNumber(document.frmEdit.elements) == false) {
        return false;
    }
    if (verifyIsDate(document.frmEdit.elements) == false) {
        return false;
    }
    if (verifyIsRate(document.frmEdit.elements) == false) {
        return false;
    }
}

//调用 this.btnSave.Attributes.Add("onclick","return BeforeSave(document.frmEdit.elements);");也可以加上checkForm()检查本表单独有的
function BeforeSave(coll) {
    if (verifyMustField(coll) == false) {
        return false;
    }
    if (verifyIsNumber(coll) == false) {
        return false;
    }
    if (verifyIsDate(coll) == false) {
        return false;
    }
    if (verifyIsRate(coll) == false) {
        return false;
    }
}

function verifyMustField(coll) {
    //alert(coll.length);
    if (coll != null) {
        for (i = 0; i < coll.length; i++) {
            //window.document.getElementsByTagName("<input") ;
            //alert(coll.item(i).id + ";" + coll.item(i).value);
            //alert(coll.item(i).outerHTML);
            //alert(coll.item(i).id + ";" + coll.item(i).mustSrc + ";" + coll.item(i).value + ";" + coll.item(i).disabled);
            //alert(coll.item(i).id + ";" + coll.item(i).mustSrc + ";" + coll.item(i).value + ";" + coll.item(i).disabled);
            //alert(coll.item(i).outerHTML.indexOf("<input"));
            //if(coll.item(i).outerHTML.indexOf("<input"))
            //alert(coll.item(i).id + ";tagName:" + coll.item(i).tagName+ ";type:" + coll.item(i).type + ";mustSrc:" + coll.item(i).getAttribute("mustSrc"));
            //if(coll.item(i).getAttribute("type").toUpperCase() == "TEXT".toUpperCase())
            //{
            //	alert(coll.item(i).id + ";tagName:" + coll.item(i).tagName+ ";type:" + coll.item(i).type + ";mustSrc:" + coll.item(i).getAttribute("mustSrc") + ";value:" + coll.item(i).value + ";disabled:" + coll.item(i).disabled);
            //}
            if (((coll.item(i).getAttribute("type").toUpperCase() == "TEXT".toUpperCase()) || (coll.item(i).getAttribute("type").toUpperCase() == "TEXTAREA".toUpperCase())) && coll.item(i).getAttribute("mustSrc") == "1" && coll.item(i).value == "" && coll.item(i).disabled == false) {
                alert("必填项 '" + GetDisplayDescription(coll.item(i)) + "' 没填写，请输入！");
                coll.item(i).focus();
                return false;
            }
        }
    }
    return true;
}

function verifyIsNumber(coll) {
    if (coll != null) {
        for (i = 0; i < coll.length; i++) {
            if (coll.item(i).numberSrc == "1" && coll.item(i).disabled == false) {
                if (coll.item(i).value - coll.item(i).value != 0) {
                    alert("数值项 '" + GetDisplayDescription(coll.item(i)) + "' 必须是数字，请修改！");
                    coll.item(i).focus();
                    return false;
                    break;
                }
            }
        }
    }
    return true;
}

function verifyIsDate(coll) {
    if (coll != null) {
        for (i = 0; i < coll.length; i++) {
            if (coll.item(i).dateSrc == "1" && coll.item(i).disabled == false) {
                if (coll.item(i).value != "") {
                    //if (!isDate(coll.item(i).value))	//普通日期较验用
                    if (!isDay(coll.item(i).value))	//中原日期格式用(8位数字)
                    {
                        alert("日期项 '" + GetDisplayDescription(coll.item(i)) + "' 必须是有效日期，请修改！");
                        coll.item(i).focus();
                        return false;
                        break;
                    }
                }
            }
        }
    }
    return true;
}

function verifyIsRate(coll) {
    if (coll != null) {
        for (i = 0; i < coll.length; i++) {
            if (coll.item(i).rateSrc == "1" && coll.item(i).disabled == false) {
                if (!isRate(coll.item(i).value)) {
                    alert("比例项 '" + GetDisplayDescription(coll.item(i)) + "' 必须是介于0到100的数字，请修改！");
                    coll.item(i).focus();
                    return false;
                    break;
                }
            }
        }
    }
    return true;
}

function OpenUploadAction(url, feature) {
    var u = url.toString();
    if (u.indexOf("?") > -1) {
        url = url + "&" + Math.random();
    }
    else {
        url = url + "?" + Math.random();
    }

    var rtn = window.showModalDialog(url, window, feature);
    if (rtn == "true") {
        //window.history.go(0);
        var hidIsRebind = document.getElementById("hidIsRebind");
        if (hidIsRebind != null) {
            hidIsRebind.value = "true";
        }
        if (window.Form1 != null) {
            window.Form1.submit();
        }
        if (window.form1 != null) {
            window.form1.submit();
        }
    }
}

function OpenAction(url, feature) {

    if (feature == "FullScreen") {
        feature = "dialogWidth:" + screen.availWidth + "px;dialogheight:" + screen.availHeight + "px;status:Modal;help:no";
    }

    var u = url.toString();
    if (u.indexOf("?") > -1) {
        url = url + "&" + Math.random();
    }
    else {
        url = url + "?" + Math.random();
    }

    var rtn = window.showModalDialog(url, window, feature);
    if (rtn == "true") {
        //window.history.go(0);
        var hidIsRebind = document.getElementById("hidIsRebind");
        if (hidIsRebind != null) {
            hidIsRebind.value = "true";
        }
        if (window.Form1 != null) {
            window.Form1.submit();
        }
        if (window.form1 != null) {
            window.form1.submit();
        }
    }
    else if (rtn != "" && rtn != null) {


        try {
            document.URL = rtn;
        }
        catch (e) {
        }
    }
}

function WindowOpen(url, width, height) {
    var u = url.toString();
    if (u.indexOf("?") > -1) {
        url = url + "&" + Math.random();
    }
    else {
        url = url + "?" + Math.random();
    }

    var rtn = window.open(url, null, "width=" + width + ",height=" + height + ",top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

function winOpen(url, width, height) {
    var openobj = window;
    if (typeof (window.dialogArguments) == "object") {
        openobj = window.dialogArguments;
    }
    if (width == null || width == "" || height == null || height == "") {
        openobj.open(url);
    }
    else {
        openobj.open(url, null, "width=" + width + ",height=" + height + ",top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
    }
}

function winOpen(url) {
    var openobj = window;
    if (typeof (window.dialogArguments) == "object") {
        openobj = window.dialogArguments;
    }
    var width = screen.availWidth;
    var height = screen.availHeight;

    if (width == null || width == "" || height == null || height == "") {
        openobj.open(url);
    }
    else {
        openobj.open(url, null, "width=" + width + ",height=" + height + ",top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
    }
}

function SetControlNecessary(isNecessary) {
    var comm = document.getElementById("txtComAuditComment");

    if (comm != undefined) {
        if (isNecessary == "true") {
            comm.removeAttribute("validateCondition");
            comm.setAttribute("validateCondition", "title=审批意见;isNecessary=1;isNumber=0;isDate=0;isDateYYYYMMDD=0;isTime=0;isShortTime=0;isRate=0;isInt=0");
        }
        else {
            comm.removeAttribute("validateCondition");
            comm.setAttribute("validateCondition", "title=审批意见;isNecessary=0;isNumber=0;isDate=0;isDateYYYYMMDD=0;isTime=0;isShortTime=0;isRate=0;isInt=0");
        }
    }
}
function CheckComAuditComment() {
    var optionsResult = document.getElementsByName('rblComAuditResult');
    for (j = 0; j < optionsResult.length; j++) {
        if (optionsResult[j].checked) {
            if (optionsResult[j].value != "1")
                SetControlNecessary("true");
            else
                SetControlNecessary("false");
            break;
        }
    }
    return true;
}

function getRadioButtonListSelectedText(element) {

    var text = "";
    var rbltable = document.getElementById(element.id);
    var rbs = rbltable.getElementsByTagName("INPUT");
    for (var i = 0; i < rbs.length; i++) {
        if (rbs[i].checked) {
            text = rbltable.cells[i].innerText;
            break;
        }
    }
    return text;
}

function getRadioButtonListSelectedTextByID(id) {

    var text = "";
    var rbltable = document.getElementById(id);
    var rbs = rbltable.getElementsByTagName("INPUT");
    for (var i = 0; i < rbs.length; i++) {
        if (rbs[i].checked) {
            text = rbltable.cells[i].innerText;
            break;
        }
    }
    return text;
}

/*数字转换为大写
*/
function ChangeNumToCapital(value) {
    var intFen, i;
    var strArr, strCheck, strFen, strDW, strNum, strBig, strNow;

    if (value == "")       //数据为空时返回"零"   
        return "零";

    if (isNaN(value))       //数据非法时提示，并返回空串   
    {
        strErr = "数据" + value + "非法！"
        alert(strErr);
        return "";
    }
    strCheck = value + ".";
    strArr = strCheck.split(".");
    strCheck = strArr[0];

    if (strCheck.length > 12)       //数据大于等于一万亿时提示无法处理   
    {
        strErr = "数据" + value + "过大，无法处理！"
        alert(strErr);
        return "";
    }

    try {
        i = 0;
        strBig = "";
        intFen = value * 100;                     //转换为以分为单位的数值   
        strFen = intFen.toString();
        strArr = strFen.split(".");
        strFen = strArr[0];
        intFen = strFen.length;             //获取长度   
        strArr = strFen.split(""); //将各个数值分解到数组内   
        while (intFen != 0)       //分解并转换   
        {
            i = i + 1;
            switch (i)                             //选择单位   
            {
                case 1: strDW = "分"; break;
                case 2: strDW = "角"; break;
                case 3: strDW = "元"; break;
                case 4: strDW = "拾"; break;
                case 5: strDW = "佰"; break;
                case 6: strDW = "仟"; break;
                case 7: strDW = "万"; break;
                case 8: strDW = "拾"; break;
                case 9: strDW = "佰"; break;
                case 10: strDW = "仟"; break;
                case 11: strDW = "亿"; break;
                case 12: strDW = "拾"; break;
                case 13: strDW = "佰"; break;
                case 14: strDW = "仟"; break;
            }
            switch (strArr[intFen - 1])                             //选择数字   
            {
                case "1": strNum = "壹"; break;
                case "2": strNum = "贰"; break;
                case "3": strNum = "叁"; break;
                case "4": strNum = "肆"; break;
                case "5": strNum = "伍"; break;
                case "6": strNum = "陆"; break;
                case "7": strNum = "柒"; break;
                case "8": strNum = "捌"; break;
                case "9": strNum = "玖"; break;
                case "0": strNum = "零"; break;
            }

            //处理特殊情况   
            strNow = strBig.split("");
            //分为零时的情况   
            if ((i == 1) && (strArr[intFen - 1] == "0"))
                strBig = "整";
            //角为零时的情况   
            else if ((i == 2) && (strArr[intFen - 1] == "0")) {         //角分同时为零时的情况   
                if (strBig != "整")
                    strBig = "零" + strBig;
            }
            //元为零的情况   
            else if ((i == 3) && (strArr[intFen - 1] == "0"))
                strBig = "元" + strBig;
            //拾－仟中一位为零且其前一位（元以上）不为零的情况时补零   
            else if ((i < 7) && (i > 3) && (strArr[intFen - 1] == "0") && (strNow[0] != "零") && (strNow[0] != "元"))
                strBig = "零" + strBig;
            //拾－仟中一位为零且其前一位（元以上）也为零的情况时跨过   
            else if ((i < 7) && (i > 3) && (strArr[intFen - 1] == "0") && (strNow[0] == "零"))
            { }
            //拾－仟中一位为零且其前一位是元且为零的情况时跨过   
            else if ((i < 7) && (i > 3) && (strArr[intFen - 1] == "0") && (strNow[0] == "元"))
            { }
            //当万为零时必须补上万字   
            else if ((i == 7) && (strArr[intFen - 1] == "0"))
                strBig = "万" + strBig;
            //拾万－仟万中一位为零且其前一位（万以上）不为零的情况时补零   
            else if ((i < 11) && (i > 7) && (strArr[intFen - 1] == "0") && (strNow[0] != "零") && (strNow[0] != "万"))
                strBig = "零" + strBig;
            //拾万－仟万中一位为零且其前一位（万以上）也为零的情况时跨过   
            else if ((i < 11) && (i > 7) && (strArr[intFen - 1] == "0") && (strNow[0] == "万"))
            { }
            //拾万－仟万中一位为零且其前一位为万位且为零的情况时跨过   
            else if ((i < 11) && (i > 7) && (strArr[intFen - 1] == "0") && (strNow[0] == "零"))
            { }
            //万位为零且存在仟位和十万以上时，在万仟间补零   
            else if ((i < 11) && (i > 8) && (strArr[intFen - 1] != "0") && (strNow[0] == "万") && (strNow[2] == "仟"))
                strBig = strNum + strDW + "万零" + strBig.substring(1, strBig.length);
            //单独处理亿位   
            else if (i == 11) {
                //亿位为零且万全为零存在仟位时，去掉万补为零   
                if ((strArr[intFen - 1] == "0") && (strNow[0] == "万") && (strNow[2] == "仟"))
                    strBig = "亿" + "零" + strBig.substring(1, strBig.length);
                //亿位为零且万全为零不存在仟位时，去掉万   
                else if ((strArr[intFen - 1] == "0") && (strNow[0] == "万") && (strNow[2] != "仟"))
                    strBig = "亿" + strBig.substring(1, strBig.length);
                //亿位不为零且万全为零存在仟位时，去掉万补为零   
                else if ((strNow[0] == "万") && (strNow[2] == "仟"))
                    strBig = strNum + strDW + "零" + strBig.substring(1, strBig.length);
                //亿位不为零且万全为零不存在仟位时，去掉万   
                else if ((strNow[0] == "万") && (strNow[2] != "仟"))
                    strBig = strNum + strDW + strBig.substring(1, strBig.length);
                //其他正常情况   
                else
                    strBig = strNum + strDW + strBig;
            }
            //拾亿－仟亿中一位为零且其前一位（亿以上）不为零的情况时补零   
            else if ((i < 15) && (i > 11) && (strArr[intFen - 1] == "0") && (strNow[0] != "零") && (strNow[0] != "亿"))
                strBig = "零" + strBig;
            //拾亿－仟亿中一位为零且其前一位（亿以上）也为零的情况时跨过   
            else if ((i < 15) && (i > 11) && (strArr[intFen - 1] == "0") && (strNow[0] == "亿"))
            { }
            //拾亿－仟亿中一位为零且其前一位为亿位且为零的情况时跨过   
            else if ((i < 15) && (i > 11) && (strArr[intFen - 1] == "0") && (strNow[0] == "零"))
            { }
            //亿位为零且不存在仟万位和十亿以上时去掉上次写入的零   
            else if ((i < 15) && (i > 11) && (strArr[intFen - 1] != "0") && (strNow[0] == "零") && (strNow[1] == "亿") && (strNow[3] != "仟"))
                strBig = strNum + strDW + strBig.substring(1, strBig.length);
            //亿位为零且存在仟万位和十亿以上时，在亿仟万间补零   
            else if ((i < 15) && (i > 11) && (strArr[intFen - 1] != "0") && (strNow[0] == "零") && (strNow[1] == "亿") && (strNow[3] == "仟"))
                strBig = strNum + strDW + "亿零" + strBig.substring(2, strBig.length);
            else
                strBig = strNum + strDW + strBig;
            strFen = strFen.substring(0, intFen - 1);
            intFen = strFen.length;
            strArr = strFen.split("");
        }
        return strBig;
    } catch (err) {
        return "";             //若失败则返回原值   
    }
}
 



